package com.kingdee.eas.custom.hr.attendance.app;

import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.org.HROrgUnitInfo;
import com.kingdee.eas.basedata.org.JobInfo;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.hr.attendance.CalendarTypeInfo;
import com.kingdee.eas.custom.hr.attendance.ScheduleChangeEntryFactory;
import com.kingdee.eas.custom.hr.attendance.ScheduleChangeEntryInfo;
import com.kingdee.eas.custom.hr.attendance.ScheduleChangeInfo;
import com.kingdee.eas.custom.hr.attendance.WorkforceManagementInfo;
import com.kingdee.eas.custom.hr.attendance.WorkingTimeZoneMgrInfo;
import com.kingdee.eas.hr.base.ApproveTypeEnum;
import com.kingdee.eas.hr.base.HRBillStateEnum;
import com.kingdee.eas.hr.base.IHRBillBaseEntry;
import com.kingdee.eas.hr.base.app.IHRBillBizBean;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;

public class ScheduleChangeControllerBean extends
		AbstractScheduleChangeControllerBean {
	private static Logger logger = Logger
			.getLogger("com.kingdee.eas.custom.hr.attendance.app.ScheduleChangeControllerBean");

	@Override
	protected IHRBillBizBean getBizBean() throws BOSException, EASBizException {
		return new ScheduleChangeBizBean();
	}

	@Override
	protected String getBillField() throws BOSException, EASBizException {
		return "bill";
	}

	@Override
	protected String getEntryEffectDateField() throws BOSException,
			EASBizException {
		return null;
	}

	@Override
	protected String getEntryField() throws BOSException, EASBizException {
		return "entrys";
	}

	@Override
	protected IHRBillBaseEntry getEntryInterface(Context ctx)
			throws BOSException, EASBizException {
		return ScheduleChangeEntryFactory.getLocalInstance(ctx);
	}

	@Override
	protected IObjectValue _getScheduleChangeBill(Context ctx, String bill)
			throws BOSException, EASBizException {

		StringBuffer buffer = new StringBuffer();

		buffer.append("/*dialect*/SELECT A.FID      AS FID,					\n");
		buffer.append("	       A.FNUMBER  AS FNUMBER,					\n");
		buffer.append("	       F.FID      AS FADMINORGUNITID,					\n");
		buffer.append("	       F.FNUMBER  AS FADMINORGUNITNUMBER,					\n");
		buffer.append("	       F.FNAME_L2 AS FADMINORGUNITNAME,					\n");
		buffer.append("	       A.FAPPLYDATE AS FAPPLYDATE,					\n");
		buffer.append("	       G.FID AS FAPPLIERID,					\n");
		buffer.append("	       G.FNUMBER AS FAPPLIERNUMBER,					\n");
		buffer.append("	       G.FNAME_L2 AS FAPPLIERNAME,					\n");
		buffer.append("	       A.FDESCRIPTION AS FREMARK,					\n");
		buffer.append("	       A.FAPPROVETYPE AS FAPPROVETYPE,					\n");
		buffer.append("	       A.FBILLSTATE AS FBILLSTATE,					\n");
		buffer.append("	       A.FHRORGUNITID AS FHRORGUNITID,				\n");
		buffer.append("	       B.FID AS FENTRYID,					\n");
		buffer.append("	       H.FID AS FPERSONID,					\n");
		buffer.append("	       H.FNUMBER AS FPERSONNUMBER,					\n");
		buffer.append("	       B.CFPERSONNAME AS FPERSONNAME,					\n");
		buffer.append("	       I.FID AS FJOBID,					\n");
		buffer.append("	       I.FNUMBER AS FJOBNUMBER,					\n");
		buffer.append("	       I.FNAME_L2 AS FJOBNAME,					\n");
		buffer.append("	       J.FID AS FAPPLYERDEPTID,					\n");
		buffer.append("	       J.FNUMBER AS FAPPLYERDEPTNUMBER,					\n");
		buffer.append("	       J.FNAME_L2 AS FAPPLYERDEPTNAME,					\n");
		buffer.append("	       K.FID AS FWORKFORCEMANAGEMID,					\n");
		buffer.append("	       K.FNUMBER AS FWORKFORCEMANAGEMNUMBER,					\n");
		buffer.append("	       K.FNAME_L2 AS FWORKFORCEMANAGEMNAME,					\n");
		buffer.append("	       B.CFWORKGROUP AS FWORKGROUP,					\n");
		buffer.append("	       B.CFCHANGEDATE AS CHANGEDATE,					\n");
		buffer.append("	       B.FDESCRIPTION AS FENTRYREMARK,					\n");
		buffer.append("	       B.FBIZDATE AS FBIZDATE,							\n");
		buffer.append("	       N.FID AS TARWORKINGTIMEZONEMGRID,					\n");
		buffer
				.append("	       N.FNUMBER AS TARWORKINGTIMEZONEMGRNUMBER,					\n");
		buffer.append("	       N.FNAME_L2 AS TARWORKINGTIMEZONEMGRNAME					\n");
		buffer.append("	  FROM CT_KQ_SCHEDULECHANGE A					\n");
		buffer.append("	  LEFT JOIN CT_KQ_SCHEDULECHANGEENTRY B					\n");
		buffer.append("	    ON A.FID = B.FBILLID					\n");
		
		buffer.append("	  LEFT JOIN T_ORG_ADMIN F					\n");
		buffer.append("	    ON A.FADMINORGUNITID = F.FID					\n");
		buffer
				.append("	  LEFT JOIN T_BD_PERSON G ON A.FAPPLIERID=G.FID					\n");
		buffer
				.append("	  LEFT JOIN T_BD_PERSON H ON B.CFPERSONID=H.FID					\n");
		buffer.append("	  LEFT JOIN T_ORG_JOB I ON B.CFJOBID=I.FID					\n");
		buffer.append("	  LEFT JOIN T_ORG_ADMIN J					\n");
		buffer.append("	    ON H.CFDEPTID = J.FID					\n");
		buffer
				.append("	  LEFT JOIN CT_KQ_WORKFORCEMANAGEMENT K ON B.CFWORKFORCEMANAGEM=K.FID					\n");
		buffer.append("	   LEFT JOIN (SELECT L.FID      AS FID,					\n");
		buffer.append("	                    L.FNUMBER  AS FNUMBER,					\n");
		buffer.append("	                    L.FNAME_L2 AS FNAME_L2					\n");
		buffer.append("	               FROM CT_KQ_CALENDARTYPE L					\n");
		buffer.append("	             UNION					\n");
		buffer.append("	             SELECT M.FID      AS FID,					\n");
		buffer.append("	                    M.FNUMBER  AS FNUMBER,					\n");
		buffer.append("	                    M.FNAME_L2 AS FNAME_L2					\n");
		buffer
				.append("	               FROM CT_KQ_WORKINGTIMEZONEMGR M) N					\n");
		buffer.append("	    ON B.CFTARGETWORKTIMEZO = N.FID					\n");
		buffer.append("		WHERE A.FID=?			\n");
		IRowSet rs = DbUtil.executeQuery(ctx, buffer.toString(),
				new Object[] { bill });

		ScheduleChangeInfo info = null;
		boolean flg = true;
		try {
			while (rs.next()) {
				if (flg) {
					info = new ScheduleChangeInfo();
					info.setId(BOSUuid.read(rs.getString("FID")));
					info.setNumber(rs.getString("FNUMBER"));
					if (rs.getString("FADMINORGUNITID") != null) {
						AdminOrgUnitInfo adminOrg = new AdminOrgUnitInfo();
						adminOrg.setId(BOSUuid.read(rs
								.getString("FADMINORGUNITID")));
						adminOrg.setNumber(rs.getString("FADMINORGUNITNUMBER"));
						adminOrg.setName(rs.getString("FADMINORGUNITNAME"));
						info.setAdminOrg(adminOrg);
					}

					info.setApplyDate(rs.getDate("FAPPLYDATE"));
					if (rs.getString("FAPPLIERID") != null) {
						PersonInfo applier = new PersonInfo();
						applier.setId(BOSUuid.read(rs.getString("FAPPLIERID")));
						applier.setNumber(rs.getString("FAPPLIERNUMBER"));
						applier.setName(rs.getString("FAPPLIERNAME"));
						info.setApplier(applier);
					}
					info.setDescription(rs.getString("FREMARK"));
					info.setApproveType(ApproveTypeEnum.getEnum(rs
							.getInt("FAPPROVETYPE")));
					info.setBillState(HRBillStateEnum.getEnum(rs
							.getInt("FBILLSTATE")));
					
					if(rs.getString("FHRORGUNITID")!=null){
						HROrgUnitInfo hrorgUnitInfo = new HROrgUnitInfo();
						hrorgUnitInfo.setId(BOSUuid.read(rs.getString("FHRORGUNITID")));
						info.setHrOrgUnit(hrorgUnitInfo);
					}
					
					flg = false;
				}

				if (rs.getString("FENTRYID") != null) {
					ScheduleChangeEntryInfo entry = new ScheduleChangeEntryInfo();
					entry.setId(BOSUuid.read(rs.getString("FENTRYID")));
					if (rs.getString("FPERSONID") != null) {
						PersonInfo person = new PersonInfo();
						person.setId(BOSUuid.read(rs.getString("FPERSONID")));
						person.setNumber(rs.getString("FPERSONNUMBER"));
						person.setName(rs.getString("FPERSONNAME"));
						entry.setPerson(person);
					}
					entry.setPersonName(rs.getString("FPERSONNAME"));
					if (rs.getString("FJOBID") != null) {
						JobInfo job = new JobInfo();
						job.setId(BOSUuid.read(rs.getString("FJOBID")));
						job.setNumber(rs.getString("FJOBNUMBER"));
						job.setName(rs.getString("FJOBNAME"));
						entry.setJob(job);
					}

					if (rs.getString("FAPPLYERDEPTID") != null) {
						AdminOrgUnitInfo adminOrg = new AdminOrgUnitInfo();
						adminOrg.setId(BOSUuid.read(rs
								.getString("FAPPLYERDEPTID")));
						adminOrg.setNumber(rs.getString("FAPPLYERDEPTNUMBER"));
						adminOrg.setName(rs.getString("FAPPLYERDEPTNAME"));
						entry.setAdminOrg(adminOrg);
					}

					if (rs.getString("FWORKFORCEMANAGEMID") != null) {
						WorkforceManagementInfo item = new WorkforceManagementInfo();
						item.setId(BOSUuid.read(rs
								.getString("FWORKFORCEMANAGEMID")));
						item.setNumber(rs.getString("FWORKFORCEMANAGEMNUMBER"));
						item.setName(rs.getString("FWORKFORCEMANAGEMNAME"));
						entry.setWorkForceManagement(item);
					}

					entry.setWorkGroup(rs.getString("FWORKGROUP"));
					entry.setChangeDate(rs.getDate("CHANGEDATE"));

					if (rs.getString("TARWORKINGTIMEZONEMGRID") != null) {

						BOSUuid id = BOSUuid.read(rs
								.getString("TARWORKINGTIMEZONEMGRID"));
						if (id.getType().toString().equals("67912D14")) {
							WorkingTimeZoneMgrInfo item = new WorkingTimeZoneMgrInfo();
							item.setId(id);
							item.setNumber(rs
									.getString("TARWORKINGTIMEZONEMGRNUMBER"));
							item.setName(rs
									.getString("TARWORKINGTIMEZONEMGRNAME"));
							entry.setTargetWorkTimeZone(item);
						} else {
							CalendarTypeInfo item = new CalendarTypeInfo();
							item.setId(id);
							item.setNumber(rs
									.getString("TARWORKINGTIMEZONEMGRNUMBER"));
							item.setName(rs
									.getString("TARWORKINGTIMEZONEMGRNAME"));
							entry.setTargetWorkTimeZone(item);
						}
					}
					entry.setBizDate(rs.getDate("FBIZDATE"));
					entry.setDescription(rs.getString("FENTRYREMARK"));

					info.getEntrys().add(entry);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return info;
	}

	@Override
	protected IObjectValue _getValue(Context ctx, IObjectPK pk,
			SelectorItemCollection selector) throws BOSException,
			EASBizException {
		return _getScheduleChangeBill(ctx, pk.toString());
	}
}